package org.darkforest.common.services.impl;

import org.darkforest.common.services.MemberService;
import org.darkforest.dao.mapper.MemberMapper;
import org.darkforest.dao.mapper.SettingMapper;
import org.darkforest.model.Member;
import org.darkforest.model.MemberWithBLOBs;
import org.darkforest.model.Setting;
import org.darkforest.vo.MemberAccount;
import org.darkforest.vo.MemberProxyVo;
import org.darkforest.vo.MemberSearchBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.List;
import java.util.Map;

/**
 * Created by Administrator on 2015/12/18.
 */
@Service(value = "memberCommonService")
public class MemberServiceImpl implements MemberService {

    @Autowired
    private MemberMapper memberMapper;
    @Autowired
    private SettingMapper settingMapper;

    @Override
    public int deleteByPrimaryKey(Integer memberId) {
        return memberMapper.deleteByPrimaryKey(memberId);
    }

    @Override
    public int insert(MemberWithBLOBs record) {
        return memberMapper.insert(record);
    }

    @Override
    public int insertSelective(MemberWithBLOBs record) {
        return memberMapper.insertSelective(record);
    }

    @Override
    public MemberWithBLOBs selectByPrimaryKey(Integer memberId) {
        return memberMapper.selectByPrimaryKey(memberId);
    }

    @Override
    public int updateByPrimaryKeySelective(MemberWithBLOBs record) {
        return memberMapper.updateByPrimaryKeySelective(record);
    }

    @Override
    public int updateByPrimaryKeyWithBLOBs(MemberWithBLOBs record) {
        return memberMapper.updateByPrimaryKeyWithBLOBs(record);
    }

    @Override
    public int updateByPrimaryKey(Member record) {
        return memberMapper.updateByPrimaryKey(record);
    }

    @Override
    public Member selectByUserName(String userName) {
        return memberMapper.selectByUserName(userName);
    }

    @Override
    public List<Member> getMemberInfoByCond(Member member) {
        return memberMapper.getMemberInfoByCond(member);
    }

    @Override
    public List<Member> getMemberPageList(MemberSearchBean param) {
        return memberMapper.getMemberPageList(param);
    }

    @Override
    public Member findMemberInfoByPrimaryKey(String recommendId) {
        return memberMapper.findMemberInfoByPrimaryKey(recommendId);
    }

    @Override
    public int updateMemberPoints(Member member) {
        return memberMapper.updateMemberPoints(member);
    }

    @Override
    public int updateMemberType(Member member) {
        return memberMapper.updateMemberType(member);
    }

    @Override
    public Integer getMemberCount(Integer nearTime) {
        return memberMapper.getMemberCount(nearTime);
    }

    @Override
    public Member getMemberById(Integer memberId) {
        return memberMapper.getMemberById(memberId);
    }

    @Override
    public MemberProxyVo getMemberProxyInfo(Integer memberId) {
        return memberMapper.getMemberProxyInfo(memberId);
    }

    @Override
    public Member getMemberByWxId(String wxOpenId) {
        return memberMapper.getMemberByWxId(wxOpenId);
    }

    @Override
    public int updateMemberBaseInfo(Member member) {
        return memberMapper.updateMemberBaseInfo(member);
    }

    @Override
    public Integer getUnderMemberCnt(Integer memberId) {
        return memberMapper.getUnderMemberCnt(memberId);
    }

    @Override
    public List<Member> getMemberByCond(Member member) {
        return memberMapper.getMemberByCond(member);
    }

    @Override
    public int updateMemberAccount(Member member) {
        return memberMapper.updateMemberAccount(member);
    }

    @Override
    public MemberAccount getMemberAccount(Integer member) {
        return memberMapper.getMemberAccount(member);
    }

    @Override
    public List<Map<String, Object>> findAddMember(MemberSearchBean memberSearchBean) {
        return memberMapper.findAddMember(memberSearchBean);
    }

    /***
     * 获取用户等级
     * @return
     */
    @Override
    public int getMemberLevel(BigDecimal careValue) {
        if(careValue == null) careValue=new BigDecimal("0");
        int level =0;
        try{
            Setting member_level_one = settingMapper.selectByPrimaryKey("member_level_one");
            BigDecimal one = member_level_one!=null&&member_level_one.getValue()!=null?new BigDecimal(member_level_one.getValue()):
                    new BigDecimal("0");
            Setting member_level_two = settingMapper.selectByPrimaryKey("member_level_two");
            BigDecimal two = member_level_two!=null&&member_level_two.getValue()!=null?new BigDecimal(member_level_two.getValue()):
                    new BigDecimal("0");
            Setting member_level_three = settingMapper.selectByPrimaryKey("member_level_three");
            BigDecimal three = member_level_three!=null&&member_level_three.getValue()!=null?new BigDecimal(member_level_three.getValue()):
                    new BigDecimal("0");
            Setting member_level_four = settingMapper.selectByPrimaryKey("member_level_four");
            BigDecimal four = member_level_four!=null&&member_level_four.getValue()!=null?new BigDecimal(member_level_four.getValue()):
                    new BigDecimal("0");
            Setting member_level_five = settingMapper.selectByPrimaryKey("member_level_five");
            BigDecimal five = member_level_five!=null&&member_level_five.getValue()!=null?new BigDecimal(member_level_five.getValue()):
                    new BigDecimal("0");
            if(careValue.compareTo(one) >=0 && careValue.compareTo(two) <0){
                level =1;
            }else if(careValue.compareTo(two) >=0 && careValue.compareTo(three) <0){
                level =2;
            }else if(careValue.compareTo(three) >=0 && careValue.compareTo(four) <0){
                level =3;
            }else if(careValue.compareTo(four) >=0 && careValue.compareTo(five) <0){
                level =4;
            }else if(careValue.compareTo(five) >=0){
                level =5;
            }
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            return level;
        }
    }
}
